﻿var _utility = {
    ajax: function (settings) {
        var that = this;
        var defaults = {
            beforeSend: function (req) {
                req.setRequestHeader("Authorization", "basic " + $.cookie("token"));
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest.status);
                if (XMLHttpRequest.status == 401) {
                    toastr.warning("身份认证失败,请重新登录");
                    window.location.href = "/login";
                    return false;
                }
            }
        };        
        $.extend(defaults, settings);
        $.ajax(defaults);
    },
    postJson: function (settings) {
        var that = this;
        var defaults = {
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            processData: false,
            beforeSend: function (req) {
                req.setRequestHeader("Authorization", "basic " + $.cookie("token"));
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest.status);
                if (XMLHttpRequest.status == 401) {
                    toastr.warning("身份认证失败,请重新登录");
                    window.location.href = "/login";
                    return false;
                }
            }
        };
        $.extend(defaults, settings);
        $.ajax(defaults);
    },
    userInfo: function (val) {
        if (val) {
            $.cookie("userinfo", JSON.stringify(val), { path: '/' });
            return val;
        } else {
            try{
                return JSON.parse($.cookie("userinfo"));
            }catch(e){
            }
        }
        return null;
    }
};

toastr.options = {
    "closeButton": true,
    "debug": false,
    "newestOnTop": true,
    "progressBar": false,
    "rtl": false,
    "positionClass": "toast-bottom-right",
    "preventDuplicates": false,
    "onclick": null,
    "showDuration": 300,
    "hideDuration": 1000,
    "timeOut": 5000,
    "extendedTimeOut": 1000,
    "showEasing": "swing",
    "hideEasing": "linear",
    "showMethod": "fadeIn",
    "hideMethod": "fadeOut"
};